using System;
using System.Threading;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using NIPO.Data;
using NIPO.Plugins;

namespace NIPOCvPlugins.DisplayOutputPlugin
{
    public class DisplayOutputPlugin : OutputPlugin
    {
        private Emgu.CV.UI.ImageViewer _viewer;
        #region Overrides of Plugin

        protected override void Initialize()
        {
        }

        public override void Dispose()
        {
            _viewer.Close();
        }

        public override bool IsConfigured
        {
            get { return true; }
        }

        #endregion

        #region Overrides of IPOPlugin

        public override void Reset()
        {
            _viewer.Dispose();
        }

        #endregion

        #region Overrides of OutputPlugin

        public override void OutputData(object sender, NIPOEventArgs e)
        {
            IImage img;
            if (e.Data is GenericDTO<Image<Bgr, byte>>)
                img = ((e.Data as GenericDTO<Image<Bgr, byte>>).Value);
            else
                img = ((e.Data as GenericDTO<Image<Gray, byte>>).Value);

            if (_viewer == null || _viewer.Image == null)
            {
                _viewer = new ImageViewer(img);
                _viewer.Show();
            }
            _viewer.Image = img;
            _viewer.Update();
            Thread.Sleep(0);
        }

        #endregion
    }
}